<?php

//------------------------------------------------------------------------------
// Class              : Ctl_Gioithieu
// Modification       :
//------------------------------------------------------------------------------
// Date(yyyy.mm.dd)   Developer                       Action
//------------------------------------------------------------------------------
// 2010.12.05         QuynhTTN                         Created
//------------------------------------------------------------------------------
// 2013.07.25         NguyenNgocManh                   Update
//------------------------------------------------------------------------------

class Ctl_Events extends ControlBase {

    var $modul = "info";

//==========================================================================
// Function    : Ctl_Gioithieu
// Creator     : QuynhTTN 2010.12.05
// Details     : Call the constructor of father class to initial variable
//==========================================================================
    function Ctl_Events() {
// Constructor of father class.
	ControlBase::ControlBase();
    }

//==========================================================================
// Function    : execute
// Argument    :
// ReturnValue :
// Creator     : QuynhTTN 2010.12.05
// Details     : Execute the process depend the given mode,
//               as: Add, Delete, Edit and default is List.
//==========================================================================
    function execute($action = "") {
	global $db;
	global $arrayError, $_POST;
	include_once DIR_MODEL . 'md_event.class.php';
	$objEvents = new MD_Event($db);
	$arrayData = array();
// Get back the work you want to do.
	$this->modul = CM_getParameter(MODUL);
	if ($action == "") {
// Get back the action: as: Add, Delete, Edit or List.
	    $action = CM_getParameter(ACTION);
	}

	switch ($action) {
	    case "Edit_type":
		$this->do_edit_events_type();
		break;
	    case "add_events_type":
		$this->do_add_events_type();
		break;
	    case "Delete_type":
		$this->do_delete_type();
		break;
	    case "Edit_events":
		$this->do_edit_events();
		break;
	    case "add_events":
		$this->do_add_events();
		break;
	    case "Del_events":
		$this->do_delete_events();
		break;
	    case "events_type":
		$arrayData = $objEvents->get_events_type_by_Where("", "", "");
		$arrayData_Event_type = "";
		include_once DIR_VIEW . 'v_event_type.php';
		V_Events_Type($arrayData, $arrayData_Event_type);
		break;
	    case "List":
	    default :
		$parent_id = CM_getParameter('parent_id');
		if ($parent_id != "") {
		    $listField = "*";
		    $where = "event_type='$parent_id'";
		    $order = " id DESC";
		    $arrayData = $objEvents->getByWhere($listField, $where, $order);
		    $where1 = " id = '$parent_id'";
		    $order1 = "";
		    $limit1 = "";
		    $arrayData_Event_type = $objEvents->get_events_type_by_Where_1($where1, $order1, $limit1);
		} else {
		    $arrayData = $objEvents->getAll("*", " ORDER BY id DESC");
		    $arrayData_Event_type = "";
		}
		include_once DIR_VIEW . 'v_events_list.php';
		V_Events_List($arrayData, $arrayData_Event_type);
		break;
	}

	unset($arrayData);
    }

    function do_delete_type() {
	global $db;
	global $arrayError, $_POST;
	$arrayData = array();
	$objEvents = new MD_Event($db);

	$id = CM_getParameter("id");
	if ($this->delete_type($id)) {
	    $ulr = "?" . MODUL . "=" . $this->modul . "&" . ACTION . "=events_type";
	    CM_gotoPage($ulr);
	} else {
	    $arrayError[] = " Có lỗi trong quá trình xóa dữ liệu.";
	}
    }

    function do_delete_events() {
	global $db;
	global $arrayError, $_POST;
	$arrayData = array();
	$objEvents = new MD_Event($db);

	$id = CM_getParameter("id");
	if ($this->delete_events($id)) {
	    $ulr = "?" . MODUL . "=" . $this->modul . "&" . ACTION . "=events_list";
	    CM_gotoPage($ulr);
	} else {
	    $arrayError[] = " Có lỗi trong quá trình xóa dữ liệu.";
	}
    }

    function delete_events() {
	global $arrayError, $_POST;
	$objEvents = new MD_Event($this->db);
	$id = CM_getParameter("id");
	$dirname = DIR_IMG_EVENT;
	$arrayCheck = $objEvents->getById2($id);
	if ($arrayCheck['image_event'] != '') {
	    unlink($dirname . $arrayCheck['image_event']);
	}
	$objEvents->delete_events($id);
	if ($objEvents->errno()) {
	    $arrayError[] = $objEvents->error();
	    return false;
	}
	return true;
    }

    function delete_type() {
	global $arrayError, $_POST;
	$objEvents = new MD_Event($this->db);
	$id = CM_getParameter("id");
	$objEvents->delete_type($id);
	if ($objEvents->errno()) {
	    $arrayError[] = $objEvents->error();
	    return false;
	}
	return true;
    }

    function do_edit_events_type() {
	global $db;
	global $arrayError, $_POST;
	$arrayData = array();
	$objEvents = new MD_Event($db);

	include_once DIR_VIEW . 'v_add_events_type.php';

	if (isset($_POST['Submit'])) {
	    if ($this->update_type()) {
		$ulr = "?" . MODUL . "=" . $this->modul . "&" . ACTION . "=events_type";
		CM_gotoPage($ulr);
	    } else {
		$arrayData = &$_POST;
		V_Add_Events_Type($arrayData, "Edit_type");
	    }
	} else {
	    $id = CM_getParameter("id");
	    $where1 = " id = '$id'";
	    $order1 = "";
	    $limit1 = "";
	    $arrayData = $objEvents->get_events_type_by_Where_1($where1, $order1, $limit1);
	    V_Add_Events_Type($arrayData, "Edit_type");
	}
    }

    function do_edit_events() {
	global $db;
	global $arrayError, $_POST;
	$arrayData = array();
	$objEvents = new MD_Event($db);

	include_once DIR_VIEW . 'v_add_events.php';

	if (isset($_POST['Submit'])) {
	    if ($this->update_events()) {
		$ulr = "?" . MODUL . "=" . $this->modul . "&" . ACTION . "=events_list";
		CM_gotoPage($ulr);
	    } else {
		$arrayData = &$_POST;
		V_Add_Events($arrayData, "Edit_events");
	    }
	} else {
	    $id = CM_getParameter("id");
	    $where1 = " id = '$id'";
	    $order1 = "";
	    $limit1 = "";
	    $arrayData = $objEvents->getById2($id, "*");
	    V_Add_Events($arrayData, "Edit_events");
	}
    }

    function do_add_events_type() {
	global $db;
	global $arrayError, $_POST;
	$arrayData = array();
	$objEvents = new MD_Event($db);

	include_once DIR_VIEW . 'v_add_events_type.php';

	if (isset($_POST['Submit'])) {
	    if ($this->insert_type()) {
		$ulr = "?" . MODUL . "=" . $this->modul . "&" . ACTION . "=events_type";
		CM_gotoPage($ulr);
	    } else {
		$arrayData = &$_POST;
		V_Add_Events_Type($arrayData, "add_events_type");
	    }
	} else {
	    V_Add_Events_Type($arrayData, "add_events_type");
	}
    }

    function do_add_events() {
	global $db;
	global $arrayError, $_POST;
	$arrayData = array();
	$objEvents = new MD_Event($db);

	include_once DIR_VIEW . 'v_add_events.php';

	if (isset($_POST['Submit'])) {
	    if ($this->insert_events()) {
		$ulr = "?" . MODUL . "=" . $this->modul . "&" . ACTION . "=events_list";
		CM_gotoPage($ulr);
	    } else {
		$arrayData = &$_POST;
		V_Add_Events($arrayData, "add_events");
	    }
	} else {
	    V_Add_Events($arrayData, "add_events");
	}
    }

    function update_type() {
	global $arrayError, $_POST, $db;
	$objEvents = new MD_Event($db);
	$objEvents->update_type(&$_POST);
	if ($objEvents->errno()) {
	    $arrayError[] = $objEvents->error();
	    return false;
	}
	return true;
    }

    function update_events() {
	global $arrayError, $_POST, $db;
	$objEvents = new MD_Event($db);
	$id = CM_getParameter("id");
	$dirname = DIR_IMG_EVENT;
	if ($_FILES["image_event"]['name'] != "") {
	    $arrayCheck = $objEvents->getById2($id);
	    if ($arrayCheck['image_event'] != '') {
		unlink($dirname . $arrayCheck['image_event']);
	    }
	    $field_name = 'image_event';
	    $this->DoUploadFileNews_ALL($dirname, $field_name);
	}
	$objEvents->update_events(&$_POST);
	if ($objEvents->errno()) {
	    $arrayError[] = $objEvents->error();
	    return false;
	}
	unset($_SESSION['Pkd_images_ALL']);
	return true;
    }

    function insert_type() {
	global $arrayError, $_POST, $db;
	$objEvents = new MD_Event($db);
	$objEvents->insert_type(&$_POST);
	if ($objEvents->errno()) {
	    $arrayError[] = $objEvents->error();
	    return false;
	}
	return true;
    }

    function insert_events() {
	global $arrayError, $_POST, $db;
	$objEvents = new MD_Event($db);
	$dirname = DIR_IMG_EVENT;
	if ($_FILES["image_event"]['name'] != "") {
	    $field_name = 'image_event';
	    $this->DoUploadFileNews_ALL($dirname, $field_name);
	}
	$objEvents->insert_events(&$_POST);
	if ($objEvents->errno()) {
	    $arrayError[] = $objEvents->error();
	    return false;
	}
	unset($_SESSION['Pkd_images_ALL']);
	return true;
    }

}

?>